package com.wei.tang.ui.me;

import com.wei.tang.AppConest;
import com.wei.tang.AppContext;
import com.wei.tang.R;
import com.wei.tang.base.BaseActivity;
import com.wei.tang.bean.UserBean;
import com.wei.tang.db.UserDao;
import com.wei.tang.util.Tlog;

import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.EditText;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 用户登录界面
 *
 */
public class ApplyTeacherActivity extends BaseActivity {

	private ApplyTeacherActivity mySelf = this;

	@InjectView(R.id.et_username)
	EditText mEtUserName;

	@InjectView(R.id.et_goodat)
	EditText mEtGoodAt;

	@InjectView(R.id.et_teachertime)
	EditText mEtTeacherTime;

	private String mUserName = "";
	private String mGoodAt = "";
	private String mTeacherTime = "";

	@Override
	public void initView() {
	}

	@Override
	public int getLayoutId() {
		return R.layout.apply_teacher_activity;
	}

	@Override
	public void initData() {
	}

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.apply_teacher_activity;
	}

	@Override
	@OnClick({ R.id.btn_apply })
	public void onClick(View v) {
		Intent intent = null;
		int id = v.getId();
		switch (id) {
		case R.id.btn_apply:
			handleLogin();
			break;
		}
	}

	private void handleLogin() {

		if (prepareForLogin()) {
			return;
		}

		mUserName = mEtUserName.getText().toString();
		mGoodAt = mEtGoodAt.getText().toString();
		mTeacherTime = mEtTeacherTime.getText().toString();

		showWaitDialog(R.string.loading);

		ApplyTask applyTask = new ApplyTask();
		applyTask.execute(0);

	}

	class ApplyTask extends AsyncTask<Integer, Integer, Integer> {
		String msg = "";
		int code = AppConest.WS_SUCCESS;

		@Override
		protected Integer doInBackground(Integer... params) {
			UserDao userDao = new UserDao(mySelf);
			UserBean ubean = AppContext.USERBEAN;
			ubean.setUser_status("t");
			int resultTag = userDao.updateUserInfo(ubean);
			if (resultTag == -1) {
				msg = "修改失败！";
				code = AppConest.WS_FALL;
			} else {
				AppContext.USERBEAN.setUser_status("t");
				AppConest.ISSTUDENT = false;
				msg = "修改成功！";
				code = AppConest.WS_SUCCESS;
			}
			return code;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			hideWaitDialog();

			if (code == AppConest.WS_FALL) {
				Tlog.e(msg);
				AppContext.showToast(msg);
				return;
			} else {
				AppContext.showToast(msg);
				mySelf.finish();
			}
		}
	}

	/**
	 * 对输入输入的数据进行检查
	 */
	private boolean prepareForLogin() {
		if (mEtUserName.length() == 0) {
			mEtUserName.setError("请输入名字");
			mEtUserName.requestFocus();
			return true;
		}
		if (mEtGoodAt.length() == 0) {
			mEtGoodAt.setError("请输入擅长方向");
			mEtGoodAt.requestFocus();
			return true;
		}
		if (mEtTeacherTime.length() == 0) {
			mEtTeacherTime.setError("请输入时间");
			mEtTeacherTime.requestFocus();
			return true;
		}
		return false;
	}
}
